﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

using ApplProtocol;

public class BattleManager : MonoBehaviour
{
    public Transform selfHero;
    public Transform copyHero;

    public Transform initLeftPosition;
    public Transform initRightPosition;


    public int syncInterval = 5; //frames

    private int frameCounter = 0;
    private Dictionary<string, Delegate> map = new Dictionary<string, Delegate>();
    private CopyHeroCtrl copyHeroCtrl;

    private void Awake()
    {
        selfHero.position =  Client.isLeft ? initLeftPosition.position : initRightPosition.position;
        copyHero.position = !Client.isLeft ? initLeftPosition.position : initRightPosition.position;

        map.Add(ApplProtocolType.position.ToString(), new Action(SetCopyHeroPosition));
        map.Add(ApplProtocolType.flip.ToString(), new Action(SwitchCopyHeroFilp));
        map.Add(ApplProtocolType.fire.ToString(), new Action(CopyHeroFire));
        map.Add(ApplProtocolType.damage.ToString(), new Action(CopyHeroDamage));
        map.Add(ApplProtocolType.exit.ToString(), new Action(EndBattle));

        copyHeroCtrl = copyHero.GetComponent<CopyHeroCtrl>();
    }

    void Update()
    {
        if (!PclObject.handled)
        {
            Debug.Log(PclObject.type);
            if (map.ContainsKey(PclObject.type))
            {
                map[PclObject.type].DynamicInvoke();
            }
            PclObject.handled = true;
        }
    }

    void FixedUpdate()
    {
        frameCounter = (frameCounter + 1) % syncInterval;

        if (frameCounter % syncInterval == 0)
        {
            //Debug.Log(selfHero.position.ToString());
            if (gameObject == null) return;
            string pclStr = PclUtils.Load(
                ApplProtocolType.position, selfHero.position.x.ToString(), selfHero.position.y.ToString()
            );

            SocketObject.SendString(pclStr);
        }
    }

    void SetCopyHeroPosition()
    {
        float x = float.Parse(PclObject.args[0]);
        float y = float.Parse(PclObject.args[1]);

        copyHero.position = new Vector2(x, y);
    }


    void SwitchCopyHeroFilp()
    {
        copyHeroCtrl.Flip();
    }

    void CopyHeroFire()
    {
        copyHeroCtrl.Fire();
    }

    void CopyHeroDamage()
    {
        copyHeroCtrl.Damage();
    }

    void EndBattle()
    {
        Client.state = Client.State.login;
        Client.isReady = false;
        SceneManager.LoadScene(1);
    }

    public static void SendFlip()
    {
        //string pclStr = PclUtils.Load(ApplProtocolType.flip);
        //SocketObject.SendString(pclStr);
    }

    public static void SendFire()
    {
        string pclStr = PclUtils.Load(ApplProtocolType.fire);
        SocketObject.SendString(pclStr);
    }

    public static void SendHeroDamage()
    {
        string pclStr = PclUtils.Load(ApplProtocolType.damage);
        SocketObject.SendString(pclStr);
    }

    public static void SendHeroDeath()
    {
        string pclStr = PclUtils.Load(ApplProtocolType.damage);
        SocketObject.SendString(pclStr);
    }

    public static void SendWinner()
    {
        string pclStr = PclUtils.Load(ApplProtocolType.winner);
        SocketObject.SendString(pclStr);
    }
}
